package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.BillRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BillMonthStatisticMapper;
import com.ruoyi.system.domain.BillMonthStatistic;
import com.ruoyi.system.service.IBillMonthStatisticService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 账单每月统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-17
 */
@Service
public class BillMonthStatisticServiceImpl implements IBillMonthStatisticService 
{
    @Resource
    private BillMonthStatisticMapper billMonthStatisticMapper;
    @Resource
    private BillRecordMapper billRecordMapper;

    /**
     * 查询账单每月统计
     * 
     * @param id 账单每月统计主键
     * @return 账单每月统计
     */
    @Override
    public BillMonthStatistic selectBillMonthStatisticById(Long id)
    {
        return billMonthStatisticMapper.selectBillMonthStatisticById(id);
    }

    /**
     * 查询账单每月统计列表
     * 
     * @param billMonthStatistic 账单每月统计
     * @return 账单每月统计
     */
    @Override
    public List<BillMonthStatistic> selectBillMonthStatisticList(BillMonthStatistic billMonthStatistic)
    {
        return billMonthStatisticMapper.selectBillMonthStatisticList(billMonthStatistic);
    }

    /**
     * 新增账单每月统计
     * 
     * @param billMonthStatistic 账单每月统计
     * @return 结果
     */
    @Override
    public int insertBillMonthStatistic(BillMonthStatistic billMonthStatistic)
    {
        billMonthStatistic.setCreateTime(DateUtils.getNowDate());
        return billMonthStatisticMapper.insertBillMonthStatistic(billMonthStatistic);
    }

    /**
     * 修改账单每月统计
     * 
     * @param billMonthStatistic 账单每月统计
     * @return 结果
     */
    @Override
    public int updateBillMonthStatistic(BillMonthStatistic billMonthStatistic)
    {
        billMonthStatistic.setUpdateTime(DateUtils.getNowDate());
        return billMonthStatisticMapper.updateBillMonthStatistic(billMonthStatistic);
    }

    /**
     * 批量删除账单每月统计
     * 
     * @param ids 需要删除的账单每月统计主键
     * @return 结果
     */
    @Override
    public int deleteBillMonthStatisticByIds(String ids)
    {
        return billMonthStatisticMapper.deleteBillMonthStatisticByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除账单每月统计信息
     * 
     * @param id 账单每月统计主键
     * @return 结果
     */
    @Override
    public int deleteBillMonthStatisticById(Long id)
    {
        return billMonthStatisticMapper.deleteBillMonthStatisticById(id);
    }

    @Override
    public void generateBillStatistic(String monthStr) {
        //如果有旧数据删除旧数据
        billMonthStatisticMapper.deleteByMonth(monthStr);
        //查询上个月末的统计信息
        BillMonthStatistic lastMonthInfo = billMonthStatisticMapper.selectLastMonthInfo(monthStr);
        //查询当月的汇总金额
        BigDecimal total = billRecordMapper.getTotalAmountByMonth(monthStr, null, null);
        //查询当月的房贷金额
        BigDecimal housingLoadAmount = billRecordMapper.getTotalAmountByMonth(monthStr, 2, 22);
        //查询当月的其他贷款金额
        BigDecimal otherLoadAmount = billRecordMapper.getTotalAmountByMonth(monthStr, 2, 23);

        //添加新的统计
        BillMonthStatistic statistic = new BillMonthStatistic();
        statistic.setStatisticMonth(monthStr);
        if(lastMonthInfo != null){
            statistic.setBeginningAmount(lastMonthInfo.getEndAmount());
            statistic.setHousingLoanAmount(lastMonthInfo.getHousingLoanAmount().add(housingLoadAmount));
            statistic.setOtherLoanAmount(lastMonthInfo.getOtherLoanAmount().add(otherLoadAmount));
        }else {
            statistic.setBeginningAmount(BigDecimal.valueOf(0));
        }
        statistic.setAmount(total);
        statistic.setEndAmount(statistic.getBeginningAmount().add(statistic.getAmount()));

        billMonthStatisticMapper.insertBillMonthStatistic(statistic);
    }
}
